package com.netease.nim.uikit.xpopup.popuview

import android.content.Context
import android.view.View
import android.widget.RadioGroup
import android.widget.TextView
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils
import www.wen.com.common.R
import www.wen.com.common.lib.data.entities.GenderSelectionType
import www.wen.com.common.lib.extension.setOnClickListenerAgency
import www.wen.com.common.lib.helper.ResultUiCallBack

/**
 * @author WEN
 * @Description:输入用户信息的弹框
 * @date 2021/3/3 14:53
 */
class GenderSelectionPopupView(
        context: Context,
        val title: CharSequence = "性别选择",
        var mGenderSelectionType: GenderSelectionType? = null,
        private val cancelContent: CharSequence = "取消",
        private val confirmContent: CharSequence = "确认",
        private val finishOnCancel: Boolean = true,
        private val resultCallBack: ResultUiCallBack<GenderSelectionType>? = null,
) : CenterPopupView(context), View.OnClickListener {

    private lateinit var cancelView: TextView

    private lateinit var enterView: TextView

    private lateinit var tvTitle: TextView

    private lateinit var genderSelectionRadioGroup: RadioGroup

    override fun getImplLayoutId(): Int = R.layout.popupview_gender_selection

    override fun onClick(v: View) {
        when (v.id) {
            R.id.cancel -> {
                if (finishOnCancel) {
                    dismiss()
                }
            }
            R.id.confirm -> {
                resultCallBack?.invoke(v, mGenderSelectionType!!, this)
                dismiss()
            }
        }
    }

    override fun initPopupContent() {
        super.initPopupContent()

        cancelView = findViewById(R.id.cancel)
        enterView = findViewById(R.id.confirm)
        genderSelectionRadioGroup = findViewById(R.id.genderSelectionRadioGroup)

        tvTitle = findViewById(R.id.tvTitle)

        tvTitle.text = title
        cancelView.text = cancelContent
        enterView.text = confirmContent

        if (mGenderSelectionType == null || mGenderSelectionType == GenderSelectionType.CONFIDENTIALITY) mGenderSelectionType = GenderSelectionType.MALE

        if (mGenderSelectionType == GenderSelectionType.MALE) {
            genderSelectionRadioGroup.check(R.id.radiobutton2)
        } else {
            genderSelectionRadioGroup.check(R.id.radiobutton3)
        }

        genderSelectionRadioGroup.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.radiobutton1 -> {
                    mGenderSelectionType = GenderSelectionType.CONFIDENTIALITY
                }
                R.id.radiobutton2 -> {
                    mGenderSelectionType = GenderSelectionType.MALE
                }
                R.id.radiobutton3 -> {
                    mGenderSelectionType = GenderSelectionType.FEMALE
                }
            }
        }
        setOnClickListenerAgency(this, cancelView, enterView)
    }

    override fun getMaxWidth(): Int {
        return (XPopupUtils.getWindowWidth(context) * 0.8f).toInt()
    }

}


